package com.guigu.guliorder.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.gulicommon.entity.pojo.res.Res;
import com.guigu.guliorder.entity.MqMessage;
import com.guigu.guliorder.entity.dto.InsertMqMessageBatchDto;
import com.guigu.guliorder.entity.dto.SelectMqMessagePagesDto;
import com.guigu.guliorder.entity.dto.UpdateMqMessageDto;
import com.guigu.guliorder.mapper.MqMessageMapper;
import com.guigu.guliorder.service.IMqMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;

import static com.guigu.gulicommon.constant.enumes.res.AddResCode.INSERT_MQ_MESSAGE_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.DeleteResCode.DELETE_MQ_MESSAGE_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.SelectResCode.SELECT_MQ_MESSAGE_SUCCESS;
import static com.guigu.gulicommon.constant.enumes.res.UpdateResCode.UPDATE_MQ_MESSAGE_SUCCESS;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 陈俊杰
 * @since 2024-08-15
 */
@Service
public class MqMessageServiceImpl extends ServiceImpl<MqMessageMapper, MqMessage> implements IMqMessageService {

    /**
     * 批量新增MQ消息(通用)
     *
     * @param dto 添加MQ消息的参数
     * @return vo 添加MQ消息的信息
     */
    @Transactional
    @Override
    public Res<MqMessage> addBatch(ArrayList<InsertMqMessageBatchDto> dto) {
        //1 创建MQ消息集合
        ArrayList<MqMessage> mqMessagesList = new ArrayList<>(dto.size());
        //2 拷贝参数到MQ消息集合
        dto.forEach(insertDto -> {
            MqMessage mqMessage = new MqMessage();
            BeanUtils.copyProperties(insertDto, mqMessage);
            mqMessagesList.add(mqMessage);
        });
        //3 批量新增
        this.saveBatch(mqMessagesList);
        return new Res<>(INSERT_MQ_MESSAGE_SUCCESS, mqMessagesList);
    }

    /**
     * 批量删除MQ消息(通用)
     *
     * @param ids 批量删除MQ消息的id集合
     * @return vo 被删除的MQ消息的信息
     */
    @Transactional
    @Override
    public Res<MqMessage> deleteByIdsBatch(String[] ids) {
        //1 数组转集合
        ArrayList<String> idList = new ArrayList<>();
        Collections.addAll(idList, ids);
        //2 批量删除
        this.removeBatchByIds(idList);
        //3 返回被删除的MQ消息的信息
        ArrayList<MqMessage> messageArrayList = new ArrayList<>();
        idList.forEach(id -> {
            MqMessage mqMessage = new MqMessage().setMessageId(id);
            messageArrayList.add(mqMessage);
        });
        return new Res<>(DELETE_MQ_MESSAGE_SUCCESS, messageArrayList);
    }

    /**
     * 修改MQ消息(通用)
     *
     * @param dto 修改MQ消息的参数
     * @return vo 修改后MQ消息的信息
     */
    @Override
    public Res<MqMessage> update(UpdateMqMessageDto dto) {
        MqMessage mqMessage = new MqMessage();
        BeanUtils.copyProperties(dto, mqMessage);
        this.updateById(mqMessage);
        return new Res<>(UPDATE_MQ_MESSAGE_SUCCESS, mqMessage);
    }

    /**
     * 分页查询MQ消息详情信息(通用)
     *
     * @param pagesDto 查询MQ消息的参数
     * @return vo MQ消息详情的分页
     */
    @Override
    public Res<MqMessage> findPages(SelectMqMessagePagesDto pagesDto) {
        Page<MqMessage> page = new Page<>(pagesDto.getPageNum(), pagesDto.getPageSize());
        Page<MqMessage> mqMessagePage = new LambdaQueryChainWrapper<>(this.baseMapper)
                .eq(pagesDto.getMessageStatus() != null, MqMessage::getMessageStatus, pagesDto.getMessageStatus())
                .eq(StringUtils.isNotBlank(pagesDto.getQueryParam()), MqMessage::getMessageId, pagesDto.getQueryParam())
                .or()
                .eq(StringUtils.isNotBlank(pagesDto.getQueryParam()), MqMessage::getToExchane, pagesDto.getQueryParam())
                .or()
                .eq(StringUtils.isNotBlank(pagesDto.getQueryParam()), MqMessage::getRoutingKey, pagesDto.getQueryParam())
                .or()
                .eq(StringUtils.isNotBlank(pagesDto.getQueryParam()), MqMessage::getClassType, pagesDto.getQueryParam())
                .or()
                .eq(StringUtils.isNotBlank(pagesDto.getQueryParam()), MqMessage::getContent, pagesDto.getQueryParam())
                .orderByDesc(MqMessage::getUpdateTime)
                .page(page);
        return new Res<>(SELECT_MQ_MESSAGE_SUCCESS, mqMessagePage);
    }
}
